package cn.itsource.org.service.impl;

import cn.itsource.basic.page.PageList;
import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    //add是特有的方法不能删  不能将公共代码拿来替换
    @Override
    @Transactional
    public void add(Department department) {
        departmentMapper.add(department);
        this.update(department);
    }


    @Override
    @Transactional
    public void update(Department department) {
        /*处理dirPath*/
        /*1.如果本身就是父级*/
        if (department.getParent() == null){
            String dirPath = "/" + department.getId();
            department.setDirPath(dirPath);

        }else {/*2.如果不是父级*/
            Long parentId = department.getParent().getId();
            Department parent = departmentMapper.loadOne(parentId);
            department.setDirPath(parent.getDirPath() + "/" + department.getId());
        }
        departmentMapper.update(department);
    }


    //获取部门树状结构 方式一双重嵌套循环  双重嵌套 效率低
    @Override
    public List<Department> getTree() {
        List<Department> all = departmentMapper.loadAll();//获取所有部门
        List<Department> result = new ArrayList<>();//声明返回集合 装一级部门
        for (Department temp : all) {
            if(temp.getParent_id() == null){//一级部门
                result.add(temp);
            }else{//再遍历所有部门 找父级部门
                for (Department department : all) {
                    if(department.getId() == temp.getParent_id()){
                        List<Department> children = department.getChildren();
                        children.add(temp);
                    }
                }
            }
        }
        return result;
    }


    //获取部门树状结构 方式二 用hashmap方式
    /*@Override
    public List<Department> getTree() {
        List<Department> all = departmentMapper.loadAll();//获取所有部门
        List<Department> result = new ArrayList<>();//声明返回集合 装一级部门

        //把所有部门放到map里面 部门id作为key，部门对象作为value
        HashMap<Long,Department> hashMap = new HashMap<>();
        for (Department department : all) {
            hashMap.put(department.getId(),department);//把所有部门放到map里面 部门id作为key，部门对象作为value
        }

        for (Department temp : all) {
            if(temp.getParent_id() == null){//一级部门
                result.add(temp);
            }else {//如果不是一级部门
                Long parent_id = temp.getParent_id();//拿到父部门的id
                Department parent = hashMap.get(parent_id);//拿到父部门对象
                parent.getChildren().add(temp);
            }
        }
        return result;
    }*/


}
